﻿using Gma.System.MouseKeyHook;
using hook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mousetouchhookwinform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Subscribe();//鼠标钩子实例化，订阅事件           
        }

        private IKeyboardMouseEvents m_GlobalHook;//钩子

        public void Subscribe()
        {
            m_GlobalHook = Hook.GlobalEvents();
            m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
        }

        private void GlobalHookMouseDownExt(object sender, MouseEventArgs e) {

            MessageBox.Show($"{e.Button}按下了！");
            if (e.Button == MouseButtons.Right) //鼠标右键按下，取消事件，释放钩子
            {
                m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
                m_GlobalHook.Dispose();
            }
        }            
    }
}
